package HibernateOrederManagement.app;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import HibernateOrderManagerment.bean.ReceptionStatus;

public class ReceptionStatusDao {
	private Session session;
	public ReceptionStatusDao(Session session){
		this.session=session;
	}
	public List<ReceptionStatus> list(){
		Query<ReceptionStatus> query=session.createQuery("from ReceptionStatus",ReceptionStatus.class);
		return query.getResultList();
	}
	public ReceptionStatus getReceptionStatusById(long id) {
		return session.get(ReceptionStatus.class, id);
	}
	public ReceptionStatus getByCode(String code){
		Query<ReceptionStatus> query =session.createQuery("from ReceptionStatus where code=:code",ReceptionStatus.class);
		query.setParameter("code", code);
		return query.getSingleResult();
	}
	public ReceptionStatus getByName(String name){
		Query<ReceptionStatus> query =session.createQuery("from ReceptionStatus where name=:name",ReceptionStatus.class);
		query.setParameter("name", name);
		return query.getSingleResult();
	}
	public ReceptionStatus listByDescription(String description){
		Query<ReceptionStatus> query =session.createQuery("from ReceptionStatus where description=:description",ReceptionStatus.class);
		query.setParameter("description", description);
		return query.getSingleResult();
	}
	public void insert(ReceptionStatus receptionStatus) {
		Transaction tx = session.beginTransaction();
		try {

			session.save(receptionStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void update(ReceptionStatus  receptionStatus){
		Transaction tx = session.beginTransaction();
		try {

			session.update( receptionStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}
	public void delete(ReceptionStatus  receptionStatus){
		Transaction tx = session.beginTransaction();
		try {
			
			session.delete( receptionStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}

}
